home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 76623,2065@compuserve.com (Bobby Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Growing Objects
- Date: 19 Jan 1996 16:55:13 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4doidh$16g@dub-news-svc-2.compuserve.com>
- References: <30FE8297.41C6@bme.jhu.edu>
- Reply-To: 76623,2065@compuserve.com (Bobby Martin)
- NNTP-Posting-Host: ad35-133.compuserve.com
- X-Newsreader: IBM NewsReader/2 v1.03
-
- In <30FE8297.41C6@bme.jhu.edu>, Harold Bien <hbien@bme.jhu.edu> writes:
- >First - thanks to all who responded to my earlier question. I
- >found all the comments helpful and was (ashamedly) surprised at the
- >number of responses.
- >
- > That said, I've decided to implement a "possible" solution which
- >has not yet been discussed. Before I begin, let me preface this with
- >the fact that I've invested quite a lot of time and effort into the
- >simulation already and thus am not very willing to switching languages.
- >Therefore, my current implementation is in C++ - my original language of
- >choice. However, I did look into SmallTalk as per one suggestion and
- >found that SmallTalk would have been better if it were not for the fact
- >that it's an interpreted language (or at least the version we have).
- >Anyway - this is all off topic.....
-
- bunch of stuff deleted...
-
- >class Cell
- >{
- > public: // These fuctions are accessible to the public
- > Cell(); /* This is the constructor - it gets called first when the
- > object is created */
- > // Run is the function which gets called from the 'outside'
- > void Run();
- > // PreMature is the function which runs when Run() is called and age<4
- > void Run_PreMature();
- > // PostMature is the function which is called when Run() is invoked
- > // and age >=4
- > void Run_PostMature();
- >
- > private:
- > void (*Cell::RunFunc)(); /* Pointer to a cell function */
- > unsigned int age;
- >};
- >
- > Cell::Cell() /* Constructor */
- >{
- > age=0; // Newborn
- > RunFunc=Run_PreMature; /* Point RunFunc to PreMature() function */
- >}
- >
- >void Cell:Run_PreMature()
- >{
- > age=age+1; /* I know: (age++), but for those who don't know C... */
- > if (age>=4) RunFunc=Run_PostMature; /* If age greater than or equal to
- >4
- > then point RunFunc to
- >PostMature()
- > */
- >}
-
- a bunch more stuff deleted :)
-
- Try this: create base class CellAge, with a pure virtual function Run
- and have child classes CellPreMature and CellPostMature. Then give Cell
- member data cellAge of the type CellWithAge* instead of the RunFunc. Then
- you can manipulate the cellAge pointer to point to an instance of CellPreMature
- or CellPostMature as appropriate, and call cellAge->Run() instead of RunFunc.
- This eliminates the need for a pointer to a function, and may offer some
- added flexibility in the future as you decide there is really another age group,
- CellDecrepit.
-
- You may find that some of your member data or methods migrate upwards
- to CellAge.
-
- Hope that helps.
-
-